import clr, sys

clr.AddReferenceByPartialName("Microsoft.Office.Interop.Outlook")
clr.AddReferenceByPartialName("office")

from Microsoft.Office.Interop import Outlook
from Microsoft import Office

from System.Reflection import *

from ActiveXObject import *
from GtdProperties import *
from Globals import *

class PropertyComboBox:
    def __init__(self, cmdbar, name, values, item):
        self.cmdbar = cmdbar
        self.name = name
        self.values = values

        item = ActiveXObject(item)
        self.gtdProperties = GtdProperties(ActiveXObject(item.getUserProperties()))

        self.ctrl = self._AddControl()

    def _AddControl(self):
        ctrl = self.cmdbar.Controls.Add(Office.Core.MsoControlType.msoControlComboBox,\
                                        Missing.Value,\
                                        Missing.Value,\
                                        self.cmdbar.Controls.Count + 1,\
                                        1)
        Office.Core._CommandBarComboBoxEvents_Event.add_Change(ctrl, self.OnPropertyChange)
        
        activeX = ActiveXObject(ctrl)
        for v in self.values:
            activeX.AddItem(v)
            
        currentValue = self.gtdProperties.__call__("get" + self.name)
        if currentValue == None:
            currentValue = "<%s>" % (self.name)
        activeX.setText(currentValue)

        return ctrl

    def GetControl(self):
        return self.ctrl

    def OnPropertyChange(self, ctrl):
        try:
            ctrl = ActiveXObject(ctrl)
            value = ctrl.getText()
            
            self.gtdProperties.__call__("set" + self.name, value)
        except:
            ReportException()
